/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package zombiedefense.framework;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import zombiedefense.input.InputManager;
import zombiedefense.util.NullRepaintManager;

/**
 *
 * @author &{user}
 */
public class ZombieDefenseStateManager extends GameStateManager{
    
    private JFrame window;
    
    private JPanel panel;
    private BufferedImage buffer;
    private SplashState splash;
    private MenuState menu;
    private GamePlayState game;
    private InputManager input;
    
    public static void main(String[] args){
	new ZombieDefenseStateManager().run();
    }
    
    public ZombieDefenseStateManager(){
	window = new JFrame("Zombie Defense");
	
	Toolkit kit = Toolkit.getDefaultToolkit();
	Dimension screen = kit.getScreenSize();
        
        window.setBounds((int)screen.getWidth()/8,(int)screen.getHeight()/8,(int)(screen.getWidth()* 3 /4),(int)(screen.getHeight()*3 /4));
	window.setResizable(false);
	window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        //install null repaint manager
        NullRepaintManager.install();
        
        //stop frame from drawing background
        Component c = window.getContentPane();
        if(c instanceof JComponent){
            ((JComponent)c).setOpaque(false);
        }
        
        //set cursor
	window.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
	
	//create game panel
	panel = new JPanel();
	panel.setOpaque(false);
	panel.setFocusable(false);
	panel.setFocusTraversalKeysEnabled(false);
	panel.setPreferredSize(window.getSize());
	window.add(panel,BorderLayout.CENTER);
	window.pack();
	
	//create screen buffer
	buffer = new BufferedImage(window.getWidth(),window.getHeight(),BufferedImage.TYPE_INT_ARGB);
	
	//create input managers
	input = new InputManager(window);
	
	//create game state objects
	splash = new SplashState(window.getSize(),input);
	menu = new MenuState(window.getSize(),input);
	game = new GamePlayState(window.getSize(),input,panel);
	
        //realize window
	window.setVisible(true);
    }
    
    /**
     * Draws the current state to the window.
     */
    public void draw() {
	Graphics2D g = buffer.createGraphics();
        
        g.clearRect(0,0,window.getWidth(),window.getHeight());
	currentState.draw(g);
        window.getLayeredPane().paintComponents(g);
	g.dispose();
        
        g = (Graphics2D)window.getContentPane().getGraphics();
	g.drawImage(buffer,0,0,null);
	g.dispose();
    }

    public GameState getGameState() {
	return game;
    }
}
